/* $Id: htable.h 16 2006-12-16 15:37:49Z judas $ */

#ifndef NIH_HTABLE_H
#define NIH_HTABLE_H 1

#include <nih/cdefs.h>

BEGIN_C_DECLS

#include <nih/array.h>
#include <nih/list.h>
#include <nih/types.h>

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

/* htable -> array -> slist -> entry */
struct nih_hentry {
	void *key;
	size_t keylen; /* if 0, it's a NULL-terminated string */
	void *value;
};

typedef size_t nih_hash_fnc(const void *key, size_t keylen);

struct nih_htable {
	struct nih_array *array;
	nih_hash_fnc *hash_fnc;
	nih_free_fnc *free_fnc;
	void *udata;
};

/* Default hash function */
extern size_t nih_hash(const void *key, size_t keylen);

extern struct nih_htable *nih_hcreate_own(size_t minlen,
                                          nih_hash_fnc *hash_fnc,
                                          nih_free_fnc *free_fnc);

extern inline struct nih_htable *nih_hcreate(size_t minlen)
{
        return nih_hcreate_own(minlen, &nih_hash, NULL);
}

extern inline struct nih_htable *nih_hcreate_deep(size_t minlen,
                                                  nih_free_fnc *free_fnc)
{
        return nih_hcreate_own(minlen, &nih_hash, free_fnc);
}

extern int nih_henter_bin(struct nih_htable *htable, const void *key,
                          size_t keylen, const void *value);

extern inline int nih_henter(struct nih_htable *htable, const char *key,
                             const void *value)
{
        return nih_henter_bin(htable, key, strlen(key), value);
}

extern int nih_hfind_bin(const struct nih_htable *htable,
                         const void *key, size_t keylen, void **value);

extern inline int nih_hfind(const struct nih_htable *htable, const char *key,
                            void **value)
{
        return nih_hfind_bin(htable, key, strlen(key), value);
}

extern int nih_hdelete_bin(struct nih_htable *htable,
                           const void *key, size_t keylen);

extern inline int nih_hdelete(struct nih_htable *htable, const char *key)
{
        return nih_hdelete_bin(htable, key, strlen(key));
}

extern int nih_hdestroy(struct nih_htable *htable);

END_C_DECLS

#endif /* NIH_HTABLE_H */

